package perf.data;

import java.util.Date;
import java.util.HashMap;

public class Run extends BaseItem<Binary,Setup> {

	private final HashMap<String, String> description = new HashMap<String, String>( );
	private Date rundate;
	private final Date adddate;

	public Run( Date date ) {
		super( date );
		adddate = date;
	}
	
	@Override
	public long getCost(int costidx) {
		long costs = 0;
		
		for ( Binary b : super.siblings() ) {
			costs += b.getCost(costidx);
		}
		return costs;
	}
	
	public void addDescription( String key, String val ) {
		description.put( key, val );
	}
	
	public String getDescription( String key ) {
		return description.get( key );
	}
	
	public void setRunDate( Date run ) {
		rundate = run;
	}
	
	public Date getRunDate( ) {
		return rundate;
	}
	
	public Date getAddDate( ) {
		return adddate;
	}
	
	public Function getFunctionInstance( String binary, String source, String function ) {
		return getBinaryInstance( binary ).getSourceInstance( source).getFunctionInstance( function ); 
	}
	
	public Binary getBinaryInstance( String name ) {
		Binary bin = findSibling( name );
		
		if ( bin != null ) {
			return bin;
		} else {
			return add( new Binary( name ) );
		}
	}
	
	@Override
	public String toString( ) {
		StringBuffer sb = new StringBuffer();
		sb.append( "Run added = " + adddate + "\n");
		sb.append( "rundate = " + rundate + "\n" );
		
		if ( description != null ) {
			for (String key : description.keySet() ) {
				sb.append( key + " = " + description.get( key ) + "\n" );
			}
		} else {
			sb.append( "No description defined\n" );
		}
		if ( siblings() != null ) {
			for (Binary b: siblings()) {
				sb.append( b.toString() );
			}
		} else {
			sb.append( "No binaries defined" );
		}
		return sb.toString();
	}
}
